package com.xz.group12306.business.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xz.group12306.business.common.DBUtils;
import com.xz.group12306.business.dao.TicketDao;
import com.xz.group12306.domain.Cart;
import com.xz.group12306.domain.Ticket;
public class TicketDaoImpl extends DBUtils implements TicketDao{

    private Connection conn;
	
	
	public TicketDaoImpl(Connection conn) {
		this.setConn(conn);
	}
	public Connection getConn() {
		return conn;
	}
	public void setConn(Connection conn) {
		this.conn = conn;
	}




	/**
	 * @author 沈姚楠
	 */
	@Override
	//根据各种条件查询车票
	public List<Ticket> findTicketList(Ticket one, int page, int rows) throws SQLException {
		// TODO Auto-generated method stub
		int start=(page-1)*rows;
		List<Ticket> tickets=new ArrayList<>();
		StringBuffer sbf=new StringBuffer();
		sbf.append("select * from tab_ticket");
		sbf.append(" where");
		//查询条件标记
				boolean tag = false;
				//根据始发地查
				String depature =one.getDepature();
				if(depature!=null && !depature.isEmpty()){
					sbf.append(" depature='"+depature+"'");
					tag = true;
				}
				
				//查询条件目的地字段
				String destinction = one.getDestinction();
				if(destinction!=null && !destinction.isEmpty()){
					sbf.append("  destinction='"+destinction+"'");
					tag = true;
				}
						
					
				sbf.append("  limit ");
				sbf.append(+start);
				sbf.append(","+rows);
				System.out.println(sbf.toString());
				//若没有查询条件则返回对象为null
						if(!tag){
							return null;
						}
						
						Ticket ticket = null;
						PreparedStatement pstmt = null;
						ResultSet rs = null;
						
						
						try {
						pstmt=conn.prepareStatement(sbf.toString());
						
						rs = pstmt.executeQuery();
						while(rs.next()) {
							// 解析结果集对象，封装查询结果
							//解析结果集对象，封装查询结果
							ticket=new Ticket();
							ticket.setTicketId(rs.getInt("ticketid"));
							ticket.setDepature(rs.getString("depature"));
							ticket.setDestinction(rs.getString("destinction"));
							ticket.setTrainNo(rs.getString("trainno"));
							ticket.setStartTime(rs.getDate("starttime"));
							ticket.setSeatNo(rs.getInt("seatno"));
							ticket.setCostTime(rs.getString("costtime"));
			                ticket.setTicketNo(rs.getInt("ticketno"));
					    
						tickets.add(ticket);
						}
						} finally {
							DBUtils.closeStatement(rs, pstmt);
						}
						
						
						return tickets;	
				
			}
	
	/**
	 * @author 胡晨倩
	 */
	//根据车票的id查询车票信息
		@Override
		public Ticket selectTicket(int ticketid) {
			// TODO Auto-generated method stub
			conn=DBUtils.getConnection();
			DBUtils.beginTransaction(conn);
			String sql="select * from tab_ticket where TicketId=?";
			Object param[]={ticketid};
			Ticket ticket=this.executeOneTicket(sql, param);
			DBUtils.closeConnection(conn);
			return ticket;
		}
		
		
		/**
		 * @author 胡晨倩
		 */
		//插入一条购买记录
		@Override
		public int save(Cart cart) {
			// TODO Auto-generated method stub
			conn=DBUtils.getConnection();
			DBUtils.beginTransaction(conn);
			String sql="insert into tab_cart(userid,ticketid,despature,destinction,costtime,starttime,trainno) values(?,?,?,?,?,?,?)";
			Object param[]={cart.getUserid(),cart.getTicketid(),cart.getDepature(),cart.getDestinction(),cart.getCostTime(),cart.getStartTime(),cart.getTrainNo()};
			int result=this.executeUpdate(sql, param);
			DBUtils.closeConnection(conn);
			return result;
		}
		
		/**
		 * @author 胡晨倩
		 */
		//修改车票数量
		@Override
		public int updateTicketNo(int ticketid) {
			// TODO Auto-generated method stub
			conn=DBUtils.getConnection();
			DBUtils.beginTransaction(conn);
			String sql="update tab_ticket set TicketNo=TicketNo-1 where TicketId=?";
			Object param[]={ticketid};
			int result=this.executeUpdate(sql, param);
			DBUtils.closeConnection(conn);
			return result;
		}

}
